package org.example.day11;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 4.1.10.2: getQueueLength() 等待锁的线程个数
 */
public class MyThread8 {
    public ReentrantLock lock = new ReentrantLock();
    public void serviceMethod(){
        try {
            System.out.println(123);
            lock.lock();
            System.out.println("ThreadName = " + Thread.currentThread().getName() + " 进入方法！");
            Thread.sleep(Integer.MAX_VALUE);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyThread8 m8 = new MyThread8();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                m8.serviceMethod();
            }
        };
        Thread[] threads = new Thread[10];
        for (int i = 0; i < 10; i++) {
            threads[i] = new Thread(runnable);
        }
        for (int i = 0; i < 10; i++) {
            threads[i].start();
        }
        Thread.sleep(2000);
        System.out.println("等待锁的线程个数: " + m8.lock.getQueueLength());
    }
}
